package com.deyuanyun.pic.settings.service.msgtemplate;

import com.deyuanyun.pic.settings.domain.base.BizMsgUser;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 消息队列
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-08-25
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class MsgQueue {

    private static BlockingQueue<PushMsgOperator> queue = new LinkedBlockingQueue<PushMsgOperator>();

    private MsgQueue() {
    }

    /**
     * 实例化消息队列工具类
     * @return
     */
    public static MsgQueue getInstance() {
        return MsgQueueHolder.msgQueue;
    }

    private static class MsgQueueHolder {
        private static MsgQueue msgQueue = new MsgQueue();
    }

    /**
     * 得到队列的头
     * @return
     */
    public PushMsgOperator get() {
        return queue == null ? null : queue.poll();
    }

    /**
     * 得到队列的大小
     * @return
     */
    public int size() {
        return queue.size();
    }

    /**
     * 从队列中删除
     * @param operator
     * @return
     */
    public boolean remove(PushMsgOperator operator){
        return queue.remove(operator);
    }

    /**
     * 添加需消息推送相关数据到队列中
     * @param operator
     */
    public void addOperator(PushMsgOperator operator) {
        if(operator != null)
            queue.offer(operator);
    }

    /**
     * 默认设置相关数据
     */
    public class MsgInfo implements Serializable {

        private static final long serialVersionUID = 8738262874001594521L;

        private Collection<String> roleIds;//角色id

        private String bizMsgThresholdId;//业务阀值id

        private String thresholdSet; //发送消息时的设置内容

        public Collection<String> getRoleIds() {
            return roleIds;
        }

        public MsgInfo setRoleIds(Collection<String> roleIds) {
            this.roleIds = roleIds;
            return this;
        }

        public String getBizMsgThresholdId() {
            return bizMsgThresholdId;
        }

        public MsgInfo setBizMsgThresholdId(String bizMsgThresholdId) {
            this.bizMsgThresholdId = bizMsgThresholdId;
            return this;
        }

        public String getThresholdSet() {
            return thresholdSet;
        }

        public MsgInfo setThresholdSet(String thresholdSet) {
            this.thresholdSet = thresholdSet;
            return this;
        }
    }

    /**
     * 用户消息相关实体类
     */
    public class BizMsgUserInfo implements Serializable {

        private static final long serialVersionUID = 8151294148379881586L;

        private boolean valid; //是否需要校验

        private Map valiMap;//验证需要的map集合

        private AbstractPushMsgTemplate pushMsgTemplate; //消息推送通用模板，方便调用验证方法

        private BizMsgUser bizMsgUser;//消息推送实体类信息

        public boolean isValid() {
            return valid;
        }

        public BizMsgUserInfo setValid(boolean valid) {
            this.valid = valid;
            return this;
        }

        public Map getValiMap() {
            return valiMap;
        }

        public BizMsgUserInfo setValiMap(Map valiMap) {
            this.valiMap = valiMap;
            return this;
        }

        public AbstractPushMsgTemplate getPushMsgTemplate() {
            return pushMsgTemplate;
        }

        public BizMsgUserInfo setPushMsgTemplate(AbstractPushMsgTemplate pushMsgTemplate) {
            this.pushMsgTemplate = pushMsgTemplate;
            return this;
        }

        public BizMsgUser getBizMsgUser() {
            return bizMsgUser;
        }

        public BizMsgUserInfo setBizMsgUser(BizMsgUser bizMsgUser) {
            this.bizMsgUser = bizMsgUser;
            return this;
        }
    }

     /**
     *  业务推送操作类
     *  @author axiao(xj08.dream@gmail.com)
     *  @date 2016-08-25
     *  @version 1.0
     *  Histroy: [修改人][时间][描述]
     */
    public class PushMsgOperator implements Serializable {

        private static final long serialVersionUID = -7632231554236489075L;

        private List<BizMsgUserInfo> userInfos;

        private MsgInfo msgInfo;

        public PushMsgOperator(List<BizMsgUserInfo> userInfos, MsgInfo msgInfo) {
            this.userInfos = userInfos;
            this.msgInfo = msgInfo;
        }

         public List<BizMsgUserInfo> getUserInfos() {
             return userInfos;
         }

         public MsgInfo getMsgInfo() {
             return msgInfo;
         }
     }

}
